#!/usr/bin/php
<?

// Mates! - Simulator reset
// Authors:
//   Ayush Agarwal <yush@umich.edu>
//   Adam Herscher <ahersche@umich.edu>
//   Jeff Powers <jrpowers@umich.edu>

// path to mates htdocs
$MATES_PATH = "/home/mates/htdocs/mates0.4";

require_once("$MATES_PATH/config.php");
require_once("$MATES_PATH/libmates.php");

// Make sure this list is kept in sync with the list in mates_simulator.php!
$users = array(
'Jacob', 'Aidan', 'Ethan', 'Ryan', 'Matthew', 'Michael', 'Tyler', 'Joshua', 'Nicholas', 'Connor', 'Zachary', 'Andrew', 'Dylan', 'Jack', 'Jayden', 'Logan', 'Caden', 'Caleb', 'Alexander', 'Nathan', 'Noah', 'William', 'Jackson', 'Joseph', 'Christopher', 'James', 'Daniel', 'Benjamin', 'Anthony', 'Cameron', 'Austin', 'Evan', 'Luke', 'Gavin', 'Brayden', 'Brandon', 'Christian', 'John', 'David', 'Sean', 'Gabriel', 'Jonathan', 'Samuel', 'Elijah', 'Colin', 'Justin', 'Alex', 'Mason', 'Jordan', 'Thomas', 'Hunter', 'Lucas', 'Kyle', 'Owen', 'Jake', 'Devin', 'Jason', 'Liam', 'Cole', 'Adam', 'Dominic', 'Aaron', 'Ian', 'Hayden', 'Isaac', 'Robert', 'Carter', 'Isaiah', 'Chase', 'Landon', 'Riley', 'Eric', 'Max', 'Nathaniel', 'Tristan', 'Brian', 'Ashton', 'Brendan', 'Cody', 'Carson', 'Julian', 'Wyatt', 'Blake', 'Seth', 'Sebastian', 'Xavier', 'Will', 'Bryce', 'Garrett', 'Kevin', 'Sam', 'Patrick', 'Brady', 'Charlie', 'Parker', 'Trevor', 'Charles', 'Cooper', 'Timothy', 'Henry', 'Emma', 'Madison', 'Emily', 'Kaitlyn', 'Hailey', 'Olivia', 'Isabella', 'Hannah', 'Sarah', 'Abigail', 'Sophia', 'Kaylee', 'Alyssa', 'Madeline', 'Brianna', 'Grace', 'Alexis', 'Sydney', 'Samantha', 'RileyB', 'Lauren', 'Taylor', 'Chloe', 'Ella', 'Ava', 'Elizabeth', 'Ashley', 'Makayla', 'Lily', 'Anna', 'Kayla', 'Megan', 'Zoe', 'Mackenzie', 'Kylie', 'Natalie', 'Allison', 'Maya', 'Katherine', 'Morgan', 'Mia', 'Jasmine', 'Isabel', 'JordanB', 'Rachel', 'Julia', 'Jessica', 'Savannah', 'Ariana', 'Victoria', 'Paige', 'Avery', 'Abby', 'Trinity', 'Brooke', 'Katie', 'Peyton', 'Alexandra', 'Jaydn', 'Rebecca', 'Jenna', 'Bailey', 'Leah', 'Gabriella', 'Faith', 'Caroline', 'Sophie', 'Nicole', 'Audrey', 'Gabrielle', 'Lillian', 'Molly', 'Ashlyn', 'Claire', 'Kennedy', 'Amelia', 'Gracie', 'Marissa', 'Amanda', 'Erin', 'Destiny', 'Reagan', 'Kate', 'Brooklyn', 'Alexa', 'Jada', 'Mary', 'Angelina', 'Ellie', 'Danielle', 'Aaliyah', 'Alicia', 'Sierra', 'Gianna', 'Kendall', 'Charlotte', 'Maria', 'Autumn', 'Shelby', 'Amber');

if (!db_connect()) // connect to the database
  die("Unable to connect to database.");

$first = 1;
foreach ($users as $user) {
  if ($first) {
    $where = "username = '$user'";
    $first = 0;
  } else
    $where .= " or username = '$user'";
}

$query = "select user_id, session_id, username from users where $where";
$res = mysql_query($query);
if (!$res)
  return die(db_err($query));
while ($row = mysql_fetch_row($res)) {
  $user_id = $row[0];
  $session_id = $row[1];
  $username = $row[2];

  // delete courses, friends, interests
  $query2 = "delete from courses where user_id = '$user_id'";
  $res2 = mysql_query($query2);
  if (!$res2)
    die(db_err($query2));
  $query2 = "delete from friends where user_id = '$user_id'";
  $res2 = mysql_query($query2);
  if (!$res2)
    die(db_err($query2));
  $query2 = "delete from interests where user_id = '$user_id'";
  $res2 = mysql_query($query2);
  if (!$res2)
    die(db_err($query2));
      
  // delete session_id
  $query2 = "update users set session_id = '' where user_id = '$user_id'";
  $res2 = mysql_query($query2);
  if (!$res2)
    die(db_err($query2));
    
  // we don't use getloc() here, because it's okay if user is not in a location
  $query2 = "select loc_id from in_loc where user_id = '$user_id'";
  $res2 = mysql_query($query2);
  if (!$res2)
    die(db_err($query2));
  
  if (mysql_num_rows($res2)) { // user has set a location
    $row2 = mysql_fetch_object($res2);
    $loc_id = $row2->loc_id;

    $exit_loc_return = exit_loc($user_id, $loc_id, $session_id, $username, 1);
    if (!$exit_loc_return[0])
      die($exit_loc_return[1]);
  }
    
  // delete queue contents (rel_queue and loc_queue happen in exit_loc)
  $query2 = "delete from msg_queue where session_id = '$session_id'";
  $res2 = mysql_query($query2);
  if (!$res2)
    die(db_err($query2));
  
  // delete the user
  $query2 = "delete from users where user_id = '$user_id'";
  $res2 = mysql_query($query2);
  if (!$res2)
    die(db_err($query2));
}
  
?>